package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.settlement.system.common.base.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
* <p>
* 天猫账单
* </p>
*
* @author huangrensen
* @since 2024-04-15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TmBill extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id")
    private String id;


    /**
    * 订单编号
    */
    private String orderNo;


    /**
    * 支付单号
    */
    private String payNo;


    /**
    * 订单状态
    */
    private String orderStatus;


    /**
    * 订单关闭原因
    */
    private String cancelReason;


    /**
    * 订单创建时间
    */
    private LocalDateTime orderCreateTime;


    /**
    * 订单付款时间
    */
    private LocalDateTime orderPayTime;


    /**
    * 发货时间
    */
    private LocalDateTime sendTime;


    /**
    * 确认收货时间
    */
    private LocalDateTime confirmReceiveTime;


    /**
    * 应发货时间
    */
    private String shouldSendTime;


    /**
    * 店铺ID
    */
    private Long storeId;

    @TableField(exist = false)
    private String tableName;
    @TableField(exist = false)
    private String billMonth;
    // 批量查询
    @TableField(exist = false)
    private List<String> orderNos;

    public static boolean isDate(String field) {
        return "orderCreateTime,orderPayTime,sendTime,confirmReceiveTime,shouldSendTime".contains(field);
    }

    public static boolean isDecimal(String field) {
        return ",buyerPayableAmount,buyerPayablePostage,buyerPayableTotalAmount,buyerPayableActAmount,sellerServiceAmount,buyerServiceAmount,purchaseRechargeAmount,purchaseRechargeQyAmount,purchaseConsumeAmount,purchaseConsumeQyAmount,refundAmount,cardDeduction,jfbDeduction,payoutsAmount,personalRedPackets,cashbackAmount,delaySendIndemnityAmount,tradePayAmount,afterSalesAmount,hbPayableAmount,creditCardPayableAmount,popularizeAmount,jhsAmount,depositAmount,".contains(","+field+",");
    }

    public static boolean isInteger(String field) {
        return "productQty".contains(field);
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date2 == null) {
            return false;
        }
        return date1 != null && date1.equals(date2);
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return value1 != null && value1.compareTo(value2) == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        TmBill tmBill = (TmBill) o;
        return Objects.equals(orderNo, tmBill.orderNo) && Objects.equals(payNo, tmBill.payNo) && Objects.equals(orderStatus, tmBill.orderStatus) && Objects.equals(cancelReason, tmBill.cancelReason) && compareDate(orderCreateTime, tmBill.orderCreateTime) && compareDate(orderPayTime, tmBill.orderPayTime) && compareDate(sendTime, tmBill.sendTime) && compareDate(confirmReceiveTime, tmBill.confirmReceiveTime) && Objects.equals(shouldSendTime, tmBill.shouldSendTime) && Objects.equals(storeId, tmBill.storeId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), orderNo, payNo, orderStatus, cancelReason, orderCreateTime, orderPayTime, sendTime, confirmReceiveTime, shouldSendTime, storeId);
    }

    // 截取长度超出范围的字段
    public void cutString() {
        if (this.cancelReason != null && this.cancelReason.length() > 500) {
            this.cancelReason = this.cancelReason.substring(0, 500);
        }
    }
}
